import {reqGetGoods,reqGetOrder,reqDeleteGoods,reqAddGoods,reqDeliverGoods} from '../api/index'
const state={
    goods:[],
    orders:[],
}
const mutations={
    // 得到所有商品
    GETGOODS(state,allgoods){
        state.goods=allgoods
    },
    // 得到所有订单
    GETORDER(state,allorder){
        state.orders=allorder
    },
    // 删除某产品
    DELETEGOODS(){
        // 删除数据后，再次发送查询请求，得到数据库最新数据
        this.dispatch('getGoods')
    },
    // 添加商品
    ADDGOODS(){
        // 添加数据后，再次发送查询请求，得到数据库最新数据
        this.dispatch('getGoods')
    },
    // 发货
    DELIVERGOODS(){
        // 发货后，再次发送查询请求，得到数据库最新数据
        this.dispatch('getOrder')
    },
}
const actions={
    // 删除某商品
    async deleteGoods({commit},id){
        let result = await reqDeleteGoods(id)
        console.log(result);
        if(result.status==200){
            commit('DELETEGOODS')
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
    // 得到所有商品
    async getGoods({commit}){
        let result = await reqGetGoods()
        // console.log(result);
        if(result.status==0){
            commit('GETGOODS',result.result)
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
    // 得到所有订单
    async getOrder({commit}){
        let result = await reqGetOrder()
        // console.log(result);
        if(result.status==200){
            commit('GETORDER',result.results)
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
    // 添加商品
    async addGoods({commit},goodsDetail){
        // console.log(goodsDetail);
        let result = await reqAddGoods(goodsDetail)
        console.log(result);
        if(result.status==200){
            commit('ADDGOODS')
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
    // 发货
    async deliverGoods({commit},orderNumber){
        console.log(orderNumber);
        let result = await reqDeliverGoods(orderNumber)
        console.log(result);
        if(result.status==200){
            commit('DELIVERGOODS')
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
}
const getters={

}
export default{
    state,
    actions,
    mutations,
    getters
}